<?php
namespace app\admin\controller;

use think\View;
use think\Db;
use think\Controller;
class Prize extends Controller
{
    //奖品列表
    public function prize_list()
    {   
    	$view       = new View();
        $prize_list = Db::name('Prize')->field('id,name,probability,count,create_time')->select();
        $sum_probability = Db::name('Prize')->value('sum(probability) as sum_probability');
        $view->assign('sum_probability',$sum_probability);
        $view->assign('list',$prize_list);

        return $view->fetch();
    }
    //编辑奖品
    public function edit(){
    	$view       = new View();

        $prize_info      = Db::name('Prize')->where('id',input('id'))->field('id,name,probability,count')->find();
        
        $view->assign('prize_info',$prize_info);

        return $view->fetch();
    }

    public function doEdit(){
    
        $prize_id = (int)$_POST['id'];
        
        $prize_info['name']        = input('name');
        $prize_info['probability'] = input('probability');
        $prize_info['count']       = input('count');
        
        $result = Db::name('Prize')->where('id',$prize_id)->update($prize_info);
        
        $this->success('修改成功', 'Prize/prize_list',1);
        
    }

    //中奖记录
    public function prize_record(){
        $view         = new View();
        $prize_record = Db::name('prize_record')
                        ->alias('pr')
                        ->join('prize p', 'pr.pid = p.id','left')
                        ->join('user u' , 'pr.mobile = u.mobile','left')
                        ->field('p.name,u.mobile,pr.create_time,pr.id')
                        ->order('pr.create_time desc')
                        ->select();

        $view->assign('list',$prize_record);

        return $view->fetch();
    }


}
